<?php


namespace app\api\controller\v1;
use app\common\model\MemberCashLog;
use app\common\model\Members as MembersModel;
use app\common\model\Business as BusinessModel;
use app\common\model\OrderGoods as OrderGoodsModel;
use app\common\model\Orders as OrdersModel;
use app\common\model\MemberBalance as MemberBalanceModel;
use app\common\model\MemberCash as MemberCashModel;
use app\common\service\Qrcode;

class Business extends Base
{
    //我的商家初始化 shipping_status 0未配送 1已配送
    public function businessInit(){
        $shipping_status = input('shipping_status');
        $page = input('page');
        $limit = input('limit');
        $orders_id = [];
        $user_id = $this->checkUserToken();
        $member = MembersModel::field('id,is_business')->append(['business_data'])->get($user_id);
        if (!$member->is_business){
            return j(1,'该用户还未成为商家');
        }
        $orders = OrdersModel::where("shipping_status",$shipping_status)->where('business_id',$member->business_data['id'])
            ->field('id')->select();
        foreach ($orders as $key => $value){
            $orders_id[] = $value['id'];
        }
        $orders_goods = OrderGoodsModel::whereIn("order_id",$orders_id)->limit(($page-1)*$limit,$limit)
            ->field('id,order_id,goods_id,goods_name,sku_name,goods_nums,image_uri,sale_price')->select();

        return j(0,'成功',[
            'data' => $orders_goods
        ]);
    }
    //商家余额初始化
    public function businessBalance(){
//        $user_id = input('user_id');
        $user_id = $this->checkUserToken();
        $member = MembersModel::field('id,balance')->get($user_id);
        $balance_log_data = MemberBalanceModel::where('userid',$user_id)->select();
        return j(0,'获取数据成功',[
            'balance' => $member->balance,
            'balance_log_data' => $balance_log_data
        ]);
    }
    //商家信息
    public function businessInfo(){
//        $user_id = input('user_id');
        $user_id = $this->checkUserToken();
        $business = BusinessModel::where("member_id",$user_id)->find();
        return j(0,'成功',$business);
    }
    //添加商家信息
    public function saveBusiness(){
//        $user_id = input('user_id');
        $user_id = $this->checkUserToken();
        $business_name = input('business_name');
        $business_icon = input('business_icon');
        $business_hours = input('business_hours');
        $business_address = input('business_address');
        $business_hotline = input('business_hotline');
        $business_advertising_picture = input('business_advertising_picture');
        $business_ambient = input('business_ambient');
        $business_introduce = input('business_introduce');
        $data = BusinessModel::where("member_id",$user_id)->find();
        if (empty($data)){
            $data = new BusinessModel();
        }
        $data->business_name = $business_name;
        $data->business_icon = $business_icon;
        $data->business_hours = $business_hours;
        $data->business_address = $business_address;
        $data->business_hotline = $business_hotline;
        $data->business_advertising_picture = $business_advertising_picture;
        $data->business_ambient = $business_ambient;
        $data->business_introduce = $business_introduce;
        $data->member_id = $user_id;
        if ($data->save()){
            return j(0,'成功');
        }
        return j(1,'失败');
    }
    //商家提现初始化
    public function businessCashInit(){
        $user_id = $this->checkUserToken();
//        $user_id = input('user_id');
        $member = MembersModel::field('id,balance')->get($user_id);
        return j(0,'获取数据成功',[
            'balance' => $member->balance,
        ]);
    }
    //立即提现 type 0微信 1银行卡
    public function withdrawal(){
        $type = input('type');
        $balance = input('balance');
        $user_id = $this->checkUserToken();
        $member = MembersModel::field('id,balance')->append(['business_id'])->get($user_id);
        if (empty($balance)){
            return j(1,'请输入提现金额');
        }
        if ($balance > $member->balance){
            return j(1,'余额不足');
        }
        $data = MemberCashModel::where("userid",$user_id)->find();
        if (empty($data)){
            return j(1,'请先绑定银行卡或微信号');
        }
        $member_cash_log = new MemberCashLog();
        //微信号
        if ($type == 0){
            if (empty($data['wecaht_number'])){
                return j(1,'请先绑定微信号');
            }
            $member_cash_log->type = 0;
        }
        //银行卡
        if ($type == 1){
            if (empty($data['card_no']) || empty($data['bank']) || empty($data['name'])){
                return j(1,'请先绑定银卡卡');
            }
            $member_cash_log->type = 1;
        }
        $member_cash_log->business_id = $member->business_id;
        $member_cash_log->user_id = $user_id;
        $member_cash_log->money   = $balance;
        $member_cash_log->cash_id = $data->id;
        if ($member_cash_log->save()){
            return  j(0,'申请提现成功');
        }
        return j(1,'申请提现失败');
    }
    //绑定银行卡
    public function bindingBankCard(){
        $user_id = $this->checkUserToken();
        $name = input('name');
        $bank = input('bank');
        $card_no = input('card_no');
        $data = MemberCashModel::where("userid",$user_id)->find();
        if (empty($data)){
            $data = new MemberCashModel();
        }
        $data->userid = $user_id;
        $data->name = $name;
        $data->bank = $bank;
        $data->card_no = $card_no;
        if ($data->save()){
            return j(0,'绑定银行卡成功');
        }
        return  j(1,"绑定银行卡失败");
    }
    //绑定微信号
    public function bindingWechatNumber(){
        $user_id = $this->checkUserToken();
        $wechat_number = input('wechat_number');
        $data = MemberCashModel::where("userid",$user_id)->find();
        if (empty($data)){
            $data = new MemberCashModel();
        }
        $data->userid = $user_id;
        $data->wechat_number = $wechat_number;
        if ($data->save()){
            return j(0,'绑定微信号成功');
        }
        return  j(1,"绑定微信号失败");
    }
    //我的推广码
    public function extensionCode(){
//        $user_id = $this->checkUserToken();
        $user_id = input('user_id');
        $member  = MembersModel::field('id,is_business,nickname,image_uri')->get($user_id);
        if (!$member->is_business){
            return j(1,'您还未成为商铺请加盟商铺');
        }
        $business = BusinessModel::where("member_id",$user_id)->field('id,business_icon,extension_code')->find();
        if ($business['extension_code']){
            return j(0,'成功',[
                'extension_code' => $business['extension_code'],
                'nickname' => $member->nickname,
                'image_uri' => $member->image_uri,
            ]);
        }
        $text = "https://www.baidu.com/";
        $logo = app()->getRootPath().attach($business['business_icon'])['attach_uri'];
        $extension_code = Qrcode::QrCodeWithLogo($text,$logo);
        $business->extension_code = $extension_code;
        if ($business->save()){
            return  j(0,'成功',[
                'extension_code' => $extension_code,
                'nickname' => $member->nickname,
                'image_uri' => $member->image_uri,
            ]);
        }
        return j(1,'失败');
    }
}